package dao

import (
	"gorm.io/gorm"
	"view-crawler-backend/internal/dao/entity"
)

func QueryAllCategories(db *gorm.DB) ([]entity.Category, error) {
	var categories []entity.Category
	if err := db.Table("category").Select("category_id", "category_name").Find(&categories).Error; err != nil {
		return nil, err
	}
	return categories, nil
}

func IsCategoriesInUse(db *gorm.DB, categoryIdList []int64) (bool, error) {
	var count int64
	if err := db.Table("task_category").Where("category_id IN ?", categoryIdList).Count(&count).Error; err != nil {
		return false, err
	}
	return count > 0, nil
}

func DeleteCategoryByIdList(db *gorm.DB, categoryIdList []int64) error {
	return db.Table("category").Where("category_id IN?", categoryIdList).Delete(&entity.Category{}).Error
}

func BatchAddCategory(db *gorm.DB, categoryList []entity.Category) error {
	return db.Table("category").Create(&categoryList).Error
}

func QueryCategoryByIdList(db *gorm.DB, categoryIdList []int64) ([]entity.Category, error) {
	var categories []entity.Category
	if err := db.Table("category").Where("category_id IN?", categoryIdList).Find(&categories).Error; err != nil {
		return nil, err
	}
	return categories, nil
}
